package cn.xbtop.topic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/6/11 14:32
 */
public class TestTopicProducer {
    public static void main(String[] args) {
        //加载IOC窗口
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-topic.xml");
        //((ClassPathXmlApplicationContext) context).start();

        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

//        jmsTemplate.setDefaultDestination(new ActiveMQQueue("activemq-queme-spring"));

        //发送消息
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                //创建消费并发送
                TextMessage textMessage = session.createTextMessage("mq-spring-topic");
                System.out.println("-----" + Thread.currentThread().getName());
                return textMessage;
            }
        });

        System.out.println("=======" + Thread.currentThread().getName());
        System.out.println("topic消息生产者发送成功---");
        //System.out.println(jmsTemplate);
    }
}
